<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务名" prop="serviceNmae">
        <el-select v-model="queryParams.serviceId" style="width:295px;" placeholder="请选择服务名,支持搜索过滤" clearable filterable @keyup.enter.native="handleQuery">
          <el-option v-for="item in serviceOptions" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          style="width:295px;"
          v-model="queryParams.name"
          placeholder="白名单名称,支持模糊查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否失效" prop="isFailure">
        <el-select v-model="queryParams.isFailure" style="width:160px;" placeholder="生效中/已失效" clearable filterable @keyup.enter.native="handleQuery">
          <el-option v-for="item in isFailureOptions" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" style="display: flex; align-items: center;">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hawk-eye:whitelist:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          style="color: red;"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hawk-eye:whitelist:remove']"
        >删除
        </el-button>
      </el-col>
      <div style="margin-left: auto;">
        <el-link
          style="font-size: 13px; color: blue;"
          :underline="false"
          @click="handleDoc">
          什么是白名单？
        </el-link>
      </div>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="whiteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="所属服务" align="center" prop="serviceName" show-overflow-tooltip/>
      <el-table-column label="名称" align="center" prop="name" show-overflow-tooltip/>
      <el-table-column label="匹配关键字" align="center" prop="matchKeyword" show-overflow-tooltip/>
      <el-table-column label="是否失效" align="center" prop="isFailure">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isFailure == 0"  type="success" effect="plain">生效中</el-tag>
          <el-tag v-else type="danger" effect="plain">已失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="失效时间" align="center" prop="failureTime"
                       :formatter="(row, column, cellValue, index) => cellValue ? cellValue : '永久生效'"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hawk-eye:whitelist:edit']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            style="color: red;"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hawk-eye:whitelist:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属服务" prop="serviceId">
              <el-select v-model="form.serviceId" placeholder="白名单作用的目标服务" clearable>
                <el-option v-for="item in serviceOptions" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="失效时间" prop="failureTime" >
              <el-date-picker
                style="width: 255px"
                v-model="form.failureTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm"
                format="yyyy-MM-dd HH:mm"
                placeholder="不选择失效时间则为永久生效">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入白名单的名称"  maxlength="30" show-word-limit/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
        <el-form-item label="匹配关键字" prop="matchKeyword">
          <el-input v-model="form.matchKeyword" type="textarea" rows="16" placeholder="匹配关键字，支持正则，可以匹配类路径或异常" maxlength="500" show-word-limit/>
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    whitePageList,
    whiteListDetail,
    addWhiteList,
    updateWhiteList,
    removeWhiteList
  } from "@/api/hawkeye/whitelist";
  import {serviceNameList} from "@/api/hawkeye/service";

  export default {
    name: "Whitelist",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        whiteList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          matchKeyword: null,
          serviceId: null,
          failureTime: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {required: true, message: "名称不能为空", trigger: "blur"}
          ],
          serviceId: [
            {required: true, message: "所属服务不能为空", trigger: "blur"}
          ],
          matchKeyword: [
            {required: true, message: "匹配关键字不能为空", trigger: "blur"}
          ]
        },
        serviceOptions: [],
        isFailureOptions: [
          {
            label: '生效中',
            value: 0
          },
          {
            label: '已失效',
            value: 1
          }
        ],
      };
    },
    created() {
      this.getList();
      this.getServiceNameList();
    },
    methods: {
      getServiceNameList() {
        serviceNameList().then(res => {
          this.serviceOptions = res.data;
        })
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        whitePageList(this.queryParams).then(res => {
          this.whiteList = res.records;
          this.total = res.total;
        }).finally(() => {
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          matchKeyword: null,
          serviceId: null,
          failureTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        whiteListDetail(id).then(res => {
          this.form = res.data;
          this.open = true;
          this.title = "修改";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWhiteList(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWhiteList(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除该数据项？').then(function () {
          return removeWhiteList(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      handleDoc() {
        let url = 'https://www.wolai.com/gMMBtvdvUL9CoBtRvCwjc2';
        if (!url) {
          return;
        }
        window.open(url);
      }
    }
  };
</script>
